@echo off
set ENV_ROOT_DIR=%~dp0..
set PREBUILD_DIR=%ENV_ROOT_DIR%\prebuild
set LIBS_DIR=%ENV_ROOT_DIR%\libs

REM 如果prebuild文件夹不存在，则创建文件夹
if not exist "%PREBUILD_DIR%" (
    echo "Create prebuild dir."
    mkdir %PREBUILD_DIR%
)

REM 如果libs文件夹不存在，则创建文件夹
if not exist "%LIBS_DIR%" (
    echo "Create libs dir."
    mkdir %LIBS_DIR%
)

REM 如果prebuild/cmake-4.1.0-rc3-windows-x86_64不存在
if not exist "%PREBUILD_DIR%\cmake-4.1.0-rc3-windows-x86_64" (
    echo "Install cmake ..."
    REM 将pkgs中的cmake-4.1.0-rc3-windows-x86_64.zip解压到prebuild目录
    powershell -Command "Expand-Archive -Path '%ENV_ROOT_DIR%\pkgs\cmake-4.1.0-rc3-windows-x86_64.zip' -DestinationPath %PREBUILD_DIR% -Force"
)

REM 如果prebuild/make-3.81-bin不存在
if not exist "%PREBUILD_DIR%\make-3.81-bin" (
    echo "Install make ..."
    REM 将pkgs中的make-3.81-bin.zip解压到prebuild目录
    powershell -Command "Expand-Archive -Path '%ENV_ROOT_DIR%\pkgs\make-3.81-bin.zip' -DestinationPath %PREBUILD_DIR% -Force"
)

REM 如果prebuild/gcc\win32\mingw64不存在
if not exist "%PREBUILD_DIR%\gcc\win32\mingw64" (
    echo "Install gcc ..."
    REM 如果gcc目录不存在
    if not exist "%PREBUILD_DIR%\gcc" (
        REM 创建gcc目录
        mkdir "%PREBUILD_DIR%\gcc"
    )
    REM 如果gcc/win32目录不存在
    if not exist "%PREBUILD_DIR%\gcc\win32" (
        REM 创建gcc/win32目录
        mkdir "%PREBUILD_DIR%\gcc\win32"
    )
    REM  使用tools/7zr.exe工具，从x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z解压gcc\win32"
    echo "Extracting gcc ..."
    "%ENV_ROOT_DIR%\tools\7zr.exe" x "%ENV_ROOT_DIR%\pkgs\x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z" -o"%PREBUILD_DIR%\gcc\win32" -y
)

REM 如果libs/SDL2-2.32.4不存在，则将pkgs/libs/SDL2-devel-2.32.4-mingw.zip解压到libs目录
if not exist "%ENV_ROOT_DIR%\libs\SDL2-2.32.4" (
    echo "Install libs ..."
    powershell -Command "Expand-Archive -Path '%ENV_ROOT_DIR%\pkgs\libs\SDL2-devel-2.32.4-mingw.zip' -DestinationPath '%ENV_ROOT_DIR%\libs' -Force"
)

REM 创建标记文件envfinished
echo "Setup finished. Create envfinished file."
type nul > %PREBUILD_DIR%\envfinished

pause